﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Reflection;

namespace SkillSystem
{
    internal class SkillManager : ISkillManager
    {
        private Dictionary<int, Type> id2RecivierType = new Dictionary<int, Type>() { { -1,typeof(SkillReceiverBase)} };

        private Dictionary<SkillItemLinkerType, Type> type2SkillLinkerType = new Dictionary<SkillItemLinkerType, Type>() {
            { SkillItemLinkerType.Combo,typeof(ComboSkillItemLinker)}
        };

        private Dictionary<DamageResultType, IDamageResultSolver> type2DamageResultSolver = new Dictionary<DamageResultType, IDamageResultSolver>() {
            { DamageResultType.ReduceHP,new DamageReduceHPSolver()}
        };

        private SkillInfo skillInfos;

        private SkillItemInfo skillItems;

        public void Init()
        {
            skillInfos = Resources.Load<SkillInfo>("ScriptableObject/SkillInfo");
            skillItems = Resources.Load<SkillItemInfo>("ScriptableObject/SkillItemInfo");
        }

        public SkillData GetSkillDataByID(int id)
        {
            for (int i = 0; i < skillInfos.Data.Count; i++)
            {
                if (skillInfos.Data[i].ID==id)
                {
                    return skillInfos.Data[i];
                } 
            }
            return null;
        }

        public SkillItemData GetSkillItemData(int id)
        {
            for (int i = 0; i < skillItems.Data.Count; i++)
            {
                if (skillItems.Data[i].ID == id)
                {
                    return skillItems.Data[i];
                }
            }
            return null;
        }

        public SkillReceiverBase GetReceiver(int id)
        {
            
            if (id2RecivierType.ContainsKey(id))
            {
                return (SkillReceiverBase)Activator.CreateInstance(id2RecivierType[id]);
            }

            return (SkillReceiverBase)Activator.CreateInstance(id2RecivierType[-1]);
        }

        public ISkillItemLinker GetItemLinker(SkillItemLinkerType linkerType)
        {
            if (type2SkillLinkerType.ContainsKey(linkerType))
            {
                return (ISkillItemLinker)Activator.CreateInstance(type2SkillLinkerType[linkerType]);
            }
            return null;
        }

        public IDamageResultSolver GetDamageResultSolver(DamageResultType resultType)
        {
            if (type2DamageResultSolver.ContainsKey(resultType))
            {
                return type2DamageResultSolver[resultType];
            }
            return null;
        }
    }
}